/**
 * 选择用户的控件
 */
$(document).ready(function(){
	var $select = $('#selectUsersAjax');
	//获取当前已经选中的，并初始化到select中
	$('input[name="selectUsersAjaxSelected"]').each(function(){
		var _this = $(this);
		$select.append("<option value='"+_this.attr('uid')+"' data-left='/images/noheader.png' data-right='"+_this.attr('logname')+"' selected >"+_this.attr('fullname')+"</option>");
		_this.remove();//删除，以免产生不必要的影响
	});
	if ($select.data('selectator') === undefined) {
		$select.selectator({
			showAllOptionsOnFocus: true,
			load: function (search, callback) {
				if (search.length < this.minSearchLength) return callback();
				$.ajax({
					url: '/users/search?q='+encodeURIComponent(search),
					type: 'GET',
					dataType: 'json',
					success: function(data) {
						callback(data);
					},
					error: function() {
						callback();
					}
				});
			},
			placeholder: '输入姓名/域账号/标签进行筛选',
			delay: 500,
			minSearchLength: 0,
			valueField: 'userId',
			textField: 'fullname',
			render: {
				selected_item: function (_item, escape) {
					var labelValue = (typeof _item.fullname !== 'undefined') ? _item.fullname : ((typeof _item.text !== 'undefined') ? _item.text : '');
		            var html = '';
		            html += '<div class="selectator_selected_item_left"><img src="/images/noheader.png"></div>';
		            html += '<div class="selectator_option_title">' + labelValue + '</div>';
		            html += '<div class="selectator_selected_item_remove">X</div>';
		            return html;
		        },
		        option: function (_item, escape) {
		        	var labelValue = (typeof _item.fullname !== 'undefined') ? _item.fullname : ((typeof _item.text !== 'undefined') ? _item.text : '');
		            var html = '';
		            html += '<div class="selectator_selected_item_left"><img src="/images/noheader.png"></div>';
		            html += '<div class="selectator_option_title">' + labelValue + '</div>';
		            return html;
		        }
			}
		});
	}
});